package 栈;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/min-stack/
 */

public class _155_最小栈 {
    public static void main(String[] args) {
        MinStack stack = new MinStack();
        stack.push(2);
        stack.push(0);
        stack.push(3);
        stack.push(2);
        System.out.println(stack.getMin());
        stack.pop();
        System.out.println(stack.getMin());
        stack.pop();
        System.out.println(stack.getMin());
        stack.pop();
        System.out.println(stack.getMin());
    }
}

/**
 * 使用两个ArrayList来对最小栈进行构建
 */
class MinStack {

    /**
     *  目标存储栈
    */
    private List<Integer> stack;

    /**
     *   存储最小值的栈
     */
    private List<Integer> minStack;

    /**
     * 初始化栈空间，将minStack存入一个MAX_VALUE
     */
    public MinStack() {
        stack = new ArrayList<>();
        minStack = new ArrayList<>();
        minStack.add(Integer.MAX_VALUE);
    }

    /**
     * 入栈操作
     * 将val存入stack中，然后比较当前最小值和入栈参数的大小，然后存入minStack
     * @param val 入栈参数
     */
    public void push(int val) {
        stack.add(val);
        minStack.add(Math.min(minStack.get(minStack.size() - 1), val));
    }

    /**
     * 删除栈元素  包括存储数据的元素以及最小值的栈空间
     */
    public void pop() {
        stack.remove(stack.size() - 1);
        minStack.remove(minStack.size() - 1);
    }

    /**
     * 获取栈顶元素
     * @return int
     */
    public int top() {
        return stack.get(stack.size() - 1);
    }

    /**
     * 获取最小值
     * 直接在minStack中提取
     * @return int
     */
    public int getMin() {return minStack.get(minStack.size() - 1);}
}
